﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SysInfo;
using CrossQuery;
using CubeDAL;

namespace CubePresentation
{
    /// <summary>
    /// Interaction logic for ConditionPanel.xaml
    /// </summary>
    public partial class ConditionPanel : UserControl
    {
        TableInfo tableInfo;
        Dictionary<string, DatabaseFieldNode> fieldsInfo;
        CrossQuery.ConditionQuery.Condition condition;
        string dataBase;
        string tableName;
        bool valid = false;
        const string comboBoxFieldsDefaultText = "选择条件字段...";
        const string comboBoxValueDefaultText = "选择值...";
        DatabaseFieldNode fieldNode;
        bool checkedChangeFormHere = true;

        public ConditionPanel(string database, string tablename, TableInfo ti)
        {
            InitializeComponent();
            tableInfo = ti;
            dataBase = database;
            tableName = tablename;
            fieldsInfo = tableInfo[tableName];
            createComboBox();
            datePickerBegin.SelectedDate = datePickerEnd.SelectedDate = DateTime.Now;
            processVisibility("None");
        }

        private void createComboBox()
        {
            comboBoxFields.Items.Add(comboBoxFieldsDefaultText);
            foreach (var pair in fieldsInfo)
            {
                comboBoxFields.Items.Add(pair.Value.DisplayName);
            }
            comboBoxFields.SelectedIndex = 0;


        }

        public CrossQuery.ConditionQuery.Condition Condition
        {
            get
            {
                int fieldno = comboBoxFields.SelectedIndex;
                if (fieldno != 0 && fieldNode != null)
                {                   
                    switch (fieldNode.DataType)
                    {
                        case "Bool":
                            {
                                List<int> selected = new List<int>();
                                selected.Add(comboBoxValue.SelectedIndex);
                                ((CrossQuery.ConditionQuery.BooleanCondition)condition).SelectedValues = selected;
                            }
                            break;
                        case "String":
                            {
                                List<int> selected = new List<int>();
                                for (int i = 2; i < comboBoxValue.Items.Count; ++i)
                                {
                                    if ((bool)((CheckBox)comboBoxValue.Items[i]).IsChecked)
                                    {
                                        selected.Add(i - 2);
                                    }
                                }
                                ((CrossQuery.ConditionQuery.MultiSelectCondition)condition).SelectedValues = selected;
                            }
                            break;
                        case "Date":
                            break;
                        case "None":
                            break;
                    }
                }
                return condition;
            }
        }

        public bool Selected
        {
            get { return (bool)checkBoxSelected.IsChecked; }
            set { checkBoxSelected.IsChecked = value; }
        }

        public bool IsValid
        {
            get
            {
                int fieldno = comboBoxFields.SelectedIndex;
                if (fieldno != 0 && fieldNode != null)
                {
                    switch (fieldNode.DataType)
                    {
                        case "Bool":
                        case "Date":
                            valid = true;
                            break;
                        case "String":
                            valid = false;
                            for (int i = 2; i < comboBoxValue.Items.Count; ++i)
                            {
                                if ((bool)((CheckBox)comboBoxValue.Items[i]).IsChecked)
                                {
                                    valid = true;
                                    break;
                                }
                            }
                            break;
                        default:
                            valid = false;
                            break;
                    }
                }
                else
                {
                    valid = false;
                }
                return valid;
            }
        }
        private void comboBoxFields_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            int fieldno = comboBoxFields.SelectedIndex;
            if (fieldno != 0)
            {
                comboBoxValue.Items.Clear();
                fieldNode = fieldsInfo.ElementAt(fieldno - 1).Value;
                switch (fieldNode.DataType)
                {
                    case "Bool":
                        condition = new CrossQuery.ConditionQuery.BooleanCondition(dataBase, tableName, fieldNode.DBFieldName);
                        foreach (var v in ((CrossQuery.ConditionQuery.BooleanCondition)condition).CandidateValues)
                        {
                            comboBoxValue.Items.Add(v);
                        }
                        if (comboBoxValue.Items.Count > 0)
                        {
                            comboBoxValue.SelectedIndex = 0;
                        }
                        break;
                    case "String":
                        condition = new CrossQuery.ConditionQuery.MultiSelectCondition(dataBase, tableName, fieldNode.DBFieldName);
                        comboBoxValue.Items.Add(comboBoxValueDefaultText);
                        CheckBox cbAll = new CheckBox();
                        cbAll.Content = "全选";
                        cbAll.Checked += (s, ee) =>
                        {
                            if (checkedChangeFormHere)
                            {
                                checkedChangeFormHere = false;
                                for (int i = 2; i < comboBoxValue.Items.Count; ++i)
                                {
                                    (comboBoxValue.Items[i] as CheckBox).IsChecked = true;
                                }
                                checkedChangeFormHere = true;
                            }
                        };
                        cbAll.Unchecked += (s, ee) =>
                        {
                            if (checkedChangeFormHere)
                            {
                                checkedChangeFormHere = false;
                                for (int i = 2; i < comboBoxValue.Items.Count; ++i)
                                {
                                    (comboBoxValue.Items[i] as CheckBox).IsChecked = false;
                                }
                                checkedChangeFormHere = true;
                            }

                        };
                        comboBoxValue.Items.Add(cbAll);
                        foreach (var v in ((CrossQuery.ConditionQuery.MultiSelectCondition)condition).CandidateValues)
                        {
                            CheckBox cb = new CheckBox();
                            cb.Content = v.DisplayValue;
                            cb.Checked += (s, ee) =>
                            {
                                if (checkedChangeFormHere)
                                {
                                    checkedChangeFormHere = false;
                                    int checkedCount = 0;
                                    for (int i = 2; i < comboBoxValue.Items.Count; ++i)
                                    {
                                        if ((bool)((CheckBox)comboBoxValue.Items[i]).IsChecked)
                                        {
                                            ++checkedCount;
                                        }
                                    }
                                    if (checkedCount == comboBoxValue.Items.Count - 2)
                                    {
                                        (comboBoxValue.Items[1] as CheckBox).IsChecked = true;
                                    }
                                    checkedChangeFormHere = true;
                                }
                            };
                            cb.Unchecked += (s, ee) =>
                            {
                                if (checkedChangeFormHere)
                                {
                                    checkedChangeFormHere = false;
                                    (comboBoxValue.Items[1] as CheckBox).IsChecked = false;
                                    checkedChangeFormHere = true;
                                }
                            };
                            comboBoxValue.Items.Add(cb);
                        }
                        comboBoxValue.SelectedIndex = 0;
                        break;
                    case "Date":
                        condition = new CrossQuery.ConditionQuery.DateCondition(dataBase, tableName, fieldNode.DBFieldName);
                        ((CrossQuery.ConditionQuery.DateCondition)condition).StartDate = datePickerBegin.SelectedDate.ToString();
                        ((CrossQuery.ConditionQuery.DateCondition)condition).EndDate = datePickerEnd.SelectedDate.ToString();
                        valid = true;
                        break;
                    default:
                        break;
                }
                processVisibility(fieldNode.DataType);
            }
            else
            {
                processVisibility("None");
            }
        
           
        }

        private void processVisibility(string type)
        {
            switch (type)
            {
                case "Bool":
                case "String":
                    label1.Content = "取值 = ";
                    label1.SetValue(Control.VisibilityProperty, Visibility.Visible);
                    label2.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    comboBoxValue.SetValue(Control.VisibilityProperty, Visibility.Visible );
                    datePickerBegin.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    datePickerEnd.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    break;
                case "Date":
                    label1.Content = "起始时间:";
                    label2.Content = "截止时间:";
                    label1.SetValue(Control.VisibilityProperty, Visibility.Visible);
                    label2.SetValue(Control.VisibilityProperty, Visibility.Visible);
                    comboBoxValue.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    datePickerBegin.SetValue(Control.VisibilityProperty, Visibility.Visible);
                    datePickerEnd.SetValue(Control.VisibilityProperty, Visibility.Visible);
                    break;
                case "None":
                default:
                    label1.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    label2.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    comboBoxValue.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    datePickerBegin.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    datePickerEnd.SetValue(Control.VisibilityProperty, Visibility.Hidden);
                    break;
            }
        }

        private void datePickerBegin_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (condition != null)
            {
                ((CrossQuery.ConditionQuery.DateCondition)condition).StartDate = ((DatePicker)sender).SelectedDate.ToString();
            }
        }

        private void datePickerEnd_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (condition != null)
            {
                ((CrossQuery.ConditionQuery.DateCondition)condition).EndDate = ((DatePicker)sender).SelectedDate.ToString();
            }
        }


        private void comboBoxValue_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            int fieldno = comboBoxFields.SelectedIndex;
            if (fieldno != 0 && fieldNode != null)
            {
                if (fieldNode.DataType.Equals("String"))
                {
                    if (comboBoxValue.SelectedIndex > 0)
                    {
                        ((CheckBox)comboBoxValue.Items[comboBoxValue.SelectedIndex]).IsChecked 
                            = !((CheckBox)comboBoxValue.Items[comboBoxValue.SelectedIndex]).IsChecked;
                    }
                    comboBoxValue.SelectedIndex = 0;
                }
            }
        }

       

    

    }
}
